﻿using SqlSugar;

namespace ServiceCore.DbContext
{
    public class SqlsugarFactory
    {
        public String _defaultConnection { get; set; }
        public SqlSugar.DbType _dataType { get; set; } = SqlSugar.DbType.MySql;
        private SqlSugarClient _privateSqlsugar { get; set; } = null;

        /// <summary>
        /// 实例
        /// </summary>
        public SqlSugarClient Db
        {
            get
            {
                if (_privateSqlsugar == null)
                {
                    _privateSqlsugar = new SqlSugarClient(
                        new ConnectionConfig()
                        {
                            ConnectionString = _defaultConnection, // 例如: "server=localhost;database=testdb;uid=root;pwd=123456;",
                            DbType = _dataType, // 根据你的数据库类型选择，例如：MySql, SqlServer, Oracle等
                            IsAutoCloseConnection = true,
                            InitKeyType = InitKeyType.Attribute // 从实体特性中读取主键自增列信息
                        }
                    );
                }
                return _privateSqlsugar;
            }
        }

        /// <summary>
        /// 构造器实例
        /// </summary>
        /// <param name="dataType">数据库类型</param>
        /// <param name="connectionString">连接字符串</param>
        public SqlsugarFactory(String connectionString)
        {
            _defaultConnection = connectionString;
        }

        /// <summary>
        /// 构造器实例
        /// </summary>
        /// <param name="dataType">数据库类型</param>
        /// <param name="connectionString">连接字符串</param>
        public SqlsugarFactory(SqlSugar.DbType dataType, String connectionString)
        {
            _dataType = dataType;
            _defaultConnection = connectionString;
        }
    }
}
